// pages/player/player.js
let nowIndex = 0;
let musicList = [];
const backgroundAudioManager = wx.getBackgroundAudioManager();
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    musicInfo:{},
    isPlay:false,
    showlyric:false,
    lyric:'',
    isSame:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    nowIndex = options.index || 0
    this._getMusicList()
  },
  /**
   * 获取音乐列表
   */
  _getMusicList() {
    musicList =  wx.getStorageSync('musicList')
    this.setData({
      musicInfo:musicList[nowIndex]
    })
    this._getMusicSrc(this.data.musicInfo.id)
    this._getlyric(this.data.musicInfo.id)
  },
  /**
   * 切换播放状态
   */
  togglePlay() {
    if (this.data.isPlay) {
      backgroundAudioManager.pause()
    } else {
      backgroundAudioManager.play()
    }
    this.setData({
      isPlay:!this.data.isPlay
    })
  },
  /**
   * 获取音乐的播放地址
   */
  async _getMusicSrc(musicId) {
    this.setData({
      isPlay:false
    })
    wx.showLoading({
      title: '加载中',
    })
    try {
      let res = await wx.cloud.callFunction({
        name:'music',
        data:{
          musicId,
          $url:'musicSrc'
        }
      })
      if (res.errMsg.indexOf('ok') > -1) {
        this._playMusic(res.result)
      } else {
        wx.showToast({
          title: '出错了,过一会再试',
        })
      }
    }catch(err) {
      console.log(`get music src fail:${err}`)
    }finally {
      wx.hideLoading()
    }
  },
  /**
   * 获取歌词
   */
  async _getlyric(musicId) {
    try {
      let res = await wx.cloud.callFunction({
        name:'music',
        data:{
          musicId,
          $url:'lyric'
        }
      })
      if (res.errMsg.indexOf('ok') > -1) {
        let lyric = ''
        if (res.result.lyric) {
          lyric = res.result.lyric
        }
       this.setData( {
           lyric
         })
      } else {
        wx.showToast({
          title: '出错了,过一会再试',
        })
      }
    }catch(err) {
      console.log(`get lyric fail:${err}`)
    }finally {
      // nothing
    }
  },
  _playMusic(result) {

    let musicInfo = this.data.musicInfo
    if (musicInfo.id === app.getPlayingMusicId()) {
      this.setData({
        isSame:true
      })
    } else {
      this.setData({
        isSame:false
      })
      app.setPlayingMusicId(Number(musicInfo.id))
      backgroundAudioManager.src = result.url
      backgroundAudioManager.title = musicInfo.name
      backgroundAudioManager.coverImgUrl = musicInfo.picUrl
      backgroundAudioManager.singer = musicInfo.ar[0].name
    }
   
    this.setData({
      isPlay:true
    })
    wx.setNavigationBarTitle({
      title: this.data.musicInfo.name,
    })
  },
  /**
   * 上一首
   */
  onPrev() {
    if (nowIndex === 0) {
      nowIndex = musicList.length - 1
    } else {
      nowIndex--
    }
    this.setData({
      musicInfo:musicList[nowIndex]
    })
    backgroundAudioManager.stop()
    this._getMusicSrc(this.data.musicInfo.id)
  },
  /**
   * 下一首
   */
  onNext() {
    if (nowIndex === musicList.length -1) {
      nowIndex = 0
    } else {
      nowIndex++
    }
    this.setData({
      musicInfo:musicList[nowIndex]
    })
    backgroundAudioManager.stop()
    this._getMusicSrc(this.data.musicInfo.id)
  },
  /**
   * 音乐开始播放
   */
  handleMusicPlay() {
    this.setData({
      isPlay:true
    })
  },
  /**
   * 音乐暂停
   */
  handleMusicPaused() {
    this.setData({
      isPlay:false
    })
  },
  /**
   * 播放时间
   */
  handleTimeUpdate(val) {
    this.selectComponent('.lyric').updateTime(val)
  },
  toggleLyric() {
    this.setData({
      showlyric:!this.data.showlyric
    })
  }
})